<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Admin_Controller {
	
	 public function __construct()
    {
        // model constructor
        parent::__construct();
		$this->layout->setLayout('login');
		$this->load->model("user");
    }
	public function index()
	{
		$data['title']="Dang nhap";
		//Không được để trống trường bắt buộc.
		//Tên nguời dùng hoặc mật khẩu không hợp lệ.
		
		$this->form_validation->set_rules('username', 'username','trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		if ($this->form_validation->run())
		{
			$params['username'] 	= $this->input->post('username');
			$params['password'] 	= $this->input->post('password');
			$check = $this->user->checkLogin($params);
			if($check){

				$this->session->set_userdata('user', $check);
				redirect('admin/dashboard');
			}else {
				$data['msg']= "The {$params['username']} field can not be the word ";
			}
			
		}
		$this->layout->view('admin/login',$data);	
	}
	
	public function checkLogin($data)
	{
		$check = $this->user->checkLogin($data);
		if ($check){
			$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
			return FALSE;
		}else{
			return TRUE;
		}
	}
	

	
	public function edit(){
		$this->layout->view('home/edit');
	}

}